package com.mkb.utils;

import com.zz.common.utils.*;
import freemarker.template.TemplateBooleanModel;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public class FreeMarkerMethod implements TemplateMethodModelEx {

	@Override
	public Object exec(List args) throws TemplateModelException {

		if (args.size() == 0) {
			throw new TemplateModelException("参数错误");
		}

		String methodName = String.valueOf(args.get(0));
		String methodValue = String.valueOf(args.get(1));
		if ("formatCustomDate".equals(methodName)) {
			String format = String.valueOf(args.get(2));
			return FreeMarkerUtil.formatCustomDate(methodValue, format);

		}
		if ("formatPercent".equals(methodName)) {
			return FreeMarkerUtil.formatPercent(methodValue);
		}
		if ("formatDate".equals(methodName)) {
			return FreeMarkerUtil.formatDate(methodValue);
		}
		if ("formatDateTimeMinute".equals(methodName)) {
			return FreeMarkerUtil.formatDateTimeMinute(methodValue);
		}
		if ("formatDateTimeSecond".equals(methodName)) {
			return FreeMarkerUtil.formatDateTimeSecond(methodValue);
		}
		if ("formatSourceUrl".equals(methodName)) {
			return FreeMarkerUtil.formatSourceUrl(methodValue);
		}
		if ("formatIndexCount".equals(methodName)) {
			return FreeMarkerUtil.formatIndexCount(methodValue);
		}
		if ("isGtNowDate".equals(methodName)) {
			return DateTool.isGtNowDate(methodValue);
		}
		if ("formatCurrency".equals(methodName)) {
			TemplateBooleanModel isHasSymbol = (TemplateBooleanModel) args.get(2);
			return MoneyCalculateUtils.formatCurrency(methodValue, isHasSymbol.getAsBoolean());
		}
		if ("formatCurrencySub".equals(methodName)) {
			String num2 = String.valueOf(args.get(2));
			int num3 = Integer.parseInt(String.valueOf(args.get(3)));
			return MoneyCalculateUtils.formatCurrency(MoneyCalculateUtils.sub(methodValue, num2, num3), false);
		}
		if ("getStringDate".equals(methodName)) {
			return DateTool.getStringDate(methodValue);
		}
		if ("divide".equals(methodName)) {
			String methodValue2 = String.valueOf(args.get(2));
			return NumberUtils.divide(NumberUtils.divide(methodValue, "10000", 2, false), methodValue2, 4, false);
		}
		if ("format".equals(methodName)) {
			return MoneyFormatTool.format(methodValue);
		}
		if ("formatChineseDate".equals(methodName)) {
			if (StringUtils.isEmpty(methodValue)) {
				return "";
			}
			return DateTool.formatChineseDate(Long.valueOf(methodValue));
		}
		if ("getDifferDay".equals(methodName)) {
			return DateTool.compareDateWithDay(methodValue);
		}
		if ("subFormatCurrency".equals(methodName)) {
			String methodValue2 = String.valueOf(args.get(2));
			int methodValue3 = Integer.parseInt(String.valueOf(args.get(3)));
			String num = MoneyCalculateUtils.sub(methodValue, methodValue2, methodValue3);
			if (StringUtils.isEmpty(num)) {
				return "";
			}
			return MoneyCalculateUtils.formatCurrency(num, false);
		}
		if ("getAmountTwo".equals(methodName)) {
			return NumberUtils.divide(methodValue, "100", 2, false);
		}
		if ("priceQty".equals(methodName)) {
			String methodValue2 = String.valueOf(args.get(2));
			return new BigDecimal(methodValue).multiply(new BigDecimal(methodValue2)).divide(new BigDecimal(1000000), 2, RoundingMode.HALF_UP).doubleValue();
		}
		if ("formatNumber".equals(methodName)) {
			return NumberUtils.formatToThousandth(methodValue, 2);
		}
		if ("priceAmount".equals(methodName)) {//价格除以100保留二位
			if (null == methodValue || StringUtils.isEmpty(methodValue)) {
				return "0.00";
			}
			return new BigDecimal(MoneyCalculateUtils.div(String.valueOf(methodValue), new BigDecimal(100).toString(), 2));
		}
		return "";
	}

}
